(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	var uid = userinfo.user_id;
	 
	$.plusReady(function() {
		var setButton = doc.getElementById('do_set');
        var oldPasswordBox = doc.getElementById('old_password');
		var passwordBox = doc.getElementById('password');
		var num = /^\d{6}$/;
		
		setButton.addEventListener('tap', function() {
			var setInfo = {
				user_id:  uid,
				old_password: oldPasswordBox.value,
				password: passwordBox.value,
				_token:userinfo.token
			};
			
			if(setInfo.old_password.trim()=='') {
				plus.nativeUI.toast('请输入旧支付密码');
				return;
			}

			if(setInfo.password.trim()=='') {
				plus.nativeUI.toast('请输入新支付密码');
				return;
			}
			
			if(setInfo.old_password.trim()==setInfo.password.trim()) {
				plus.nativeUI.toast('旧支付密码与新支付密码一致');
				return;
			}
			
			if(!num.exec(setInfo.password)){
           		  plus.nativeUI.toast('支付密码只能为6位纯数字！');
           		  return false;
           	 }
		
			mui.ajax({
				url: apiurl + "User/set_tradepwd/",
				data: setInfo,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}

					// 请求成功
					if(res.status ==0) {
						mui.toast(res.msg);
					} else {
						mui.alert(res.msg);
						mui.back();
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('修改支付密码失败！');
				}
			});
			
		}, false);
		
		
	})
	
}(mui, document));